package com.damon;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.damon.Entity.WcsDvcEntity;
import com.damon.log.WcsLog;
import com.damon.thread.SocketThread;
import com.damon.utils.DbUtil;
import com.damon.utils.GlobalOjbectUtil;
import com.damon.utils.LogUtil;
import com.damon.window.WcsWindow;

public class WcsSocketRun {
	private static WcsLog logger;

	public static String dvcTp;// 根据程序名称匹配数据中的plc设备信息

	public static void main(String[] args) throws Exception {
		WcsWindow window = new WcsWindow();
		GlobalOjbectUtil.setWindow(window);// 将可视化实例对象保存为静态对象，给多个类使用。
		logger = LogUtil.getLogger(WcsSocketRun.class);
//		dvcTp = "dWCS-LABEL-API";
		dvcTp = "WCS-SOCKET";
//		dvcTp = "PLCINTERFACE_CV";
//		dvcTp = getOwnName();// 得到程序自己的名字
		window.setTitle(dvcTp);

		logger.info("程序名称： " + dvcTp);
		logger.info("正在查找待连接设备。。。。。");
		List<WcsDvcEntity> wcsDvcList = getWcsDvcEntityList();
		logger.info("程序：" + dvcTp + ",总共查找到：" + wcsDvcList.size() + "个待连接设备！！");
		for (WcsDvcEntity wcs : wcsDvcList) {
			// 该线程是为了建立多个ip:port的socket连接
			new SocketThread(wcs).start();
		}
	}

	/**
	 * 从数据库中查找ip、port、procedure信息
	 * 
	 * @return
	 */
	public static List<WcsDvcEntity> getWcsDvcEntityList() {
		final String IS_OK = "1";

		List<WcsDvcEntity> wcsDvcList = new ArrayList<>();
		Connection conn = null;
		try {
			conn = DbUtil.getConnection();
//			String sql = "select * from wcs_dvc where isok=? and dvc_tp=?";
			String sql = "select * from wcs_dvc where is_ok=?";
			PreparedStatement preparedStatement = conn.prepareStatement(sql);
			preparedStatement.setString(1, IS_OK);
//			preparedStatement.setString(2, dvcTp);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				WcsDvcEntity wcs = new WcsDvcEntity();
				wcs.setDvcId(rs.getInt("dvc_id"));
				wcs.setDvcNo(rs.getString("dvc_no"));
				wcs.setDvcDesc(rs.getString("dvc_desc"));
				wcs.setDvcTp(rs.getString("dvc_tp"));
				wcs.setDvcIp(rs.getString("dvc_ip"));
				wcs.setPort(rs.getString("port"));
				wcs.setRevBegin(rs.getString("rev_begin"));
				wcs.setRevEnd(rs.getString("rev_end"));
				wcs.setRevProc(rs.getString("rev_proc"));
				wcs.setSendProc(rs.getString("send_proc"));
				wcs.setSendTime(rs.getInt("send_time"));
				wcs.setIsok(rs.getString("is_ok"));
				wcsDvcList.add(wcs);
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取ip:port、procedure信息失败！！",e);
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error("connection连接关闭失败！",e);
				e.printStackTrace();
			}
		}
		return wcsDvcList;
	}
	/**
	 *  ps：通过jar包运行时，获取文件名，例：test.jar
	 * @return
	 */
	public static String getOwnName() {
		// 如下代码获得：String path2 = System.getProperty("java.class.path");
		//D:\java\workspace\produce\target\produce-0.0.1-SNAPSHOT-jar-with-dependencies.jar
		
		// 如下代码获得：String path = WcsSocketRun.class.getProtectionDomain().getCodeSource().getLocation().getFile();
		// /D:/java/workspace/produce/target/produce-0.0.1-SNAPSHOT-jar-with-dependencies.jar
		
//		String path = WcsSocketRun.class.getProtectionDomain().getCodeSource().getLocation().getFile();
		String path = System.getProperty("java.class.path");
		String ownName = null;
		if (path.endsWith(".jar")) {
			ownName = path.substring(path.lastIndexOf("\\") + 1, path.length() - 4);
		} else {
			ownName = "程序非jar类型";
		}
		return ownName;
	}

}
